--[[
    手牌点数指示器
]]--

local blaze = require "blaze"
local game = require "club.threecard.blackjack"
local poker_amount = game.util.poker_amount

local log = blaze.logging.get("PointIndicator")

local M = game.ui.element()

function M:ctor(poker_container)
    self.poker_container = poker_container
end

function M:on_set_gameobject()
    self:super("on_set_gameobject")
    self.loaded = true
end

function M:update()
    self:lazy("on_update")
end

function M:on_update()
    local hand = self.poker_container.hand
    local amount = 0
    local amount_txt
    local values = poker_amount(hand)

    if #values == 2 then
        for _, value in ipairs(values[2]) do
            amount = amount + value
        end
        local v1 = 0
        local v2 = 0
        for _, v in ipairs(values[1]) do
            v1 = v1 + v
        end
        for _, v in ipairs(values[2]) do
            v2 = v2 + v
        end
        amount_txt = string.format("%s/%s", v1, v2)
    else
        for _, value in ipairs(values[1]) do
            amount = amount + value
        end
        amount_txt = tostring(amount)
    end

    if amount == 0 then
        self:hide()
        return
    end

    local player = game.kbe.player()
    if not player or not player.seat then
        return
    end
    local me = player.seat.no == self.poker_container.no
    local txt = me and self.me_txt or self.others_txt
    if amount == 21 then
        if #hand == 2 then
            -- 黑杰克21点
            self.blackjack_obj:SetActive(true)
            self.others_obj:SetActive(false)
            self.me_obj:SetActive(false)
            self.bust_obj:SetActive(false)
        else
            -- 普通21点
            self.blackjack_obj:SetActive(false)
            self.others_obj:SetActive(not me)
            self.me_obj:SetActive(me)
            self.bust_obj:SetActive(false)
        end
    elseif amount > 21 then
        -- 爆了
        self.blackjack_obj:SetActive(false)
        self.others_obj:SetActive(false)
        self.me_obj:SetActive(false)
        self.bust_obj:SetActive(true)
        txt = self.bust_txt
    else
        self.blackjack_obj:SetActive(false)
        self.others_obj:SetActive(not me)
        self.me_obj:SetActive(me)
        self.bust_obj:SetActive(false)
    end
    txt.text = tostring(amount_txt)
    self:show()
end

return M